#ifndef COMMONOPTIONS_H
#define COMMONOPTIONS_H

#include <math.h>
#include <QtXml>

class CommonOptions {
private:
    unsigned short beforeTime;
    unsigned short forwardTime;
    unsigned short backwardTime;
    bool forwardAsBefore;
    unsigned short expTime;
    unsigned short idleTime;
    unsigned short ionizationVoltage;
    unsigned short CPVoltage;
    unsigned short heatCurrent;
    unsigned short emissionCurrent;
    unsigned short focusVoltage1;
    unsigned short focusVoltage2;

    double iVoltageConvert(unsigned short voltage) const {
        return (double)(150 * (double)voltage / 4096);
    }
    double CPConvert(unsigned short coeff) const {
        return (double)((10 / (double)coeff) * 4096);
    }
    double eCurrentConvert(unsigned short current) const {
        return (double)((50 * (double)current) / 4096);
    }
    double hCurrentConvert(unsigned short current) const {
        return (double)((double)current / 4096);
    }
    double fVConvert(unsigned short voltage) const {
        return (double)(150 * (double)voltage / 4096);
    }
    unsigned short scanVoltage(unsigned short step) const {
        if (step > 1056) step = 1056;
        return (unsigned short)(4095 * pow(((double)527 / (double)528), 1056 - step));
    }
public:
    unsigned short befTimeReal() const {
        return (unsigned short)(beforeTime * 5);
    }
    unsigned short fTimeReal() const {
        return (unsigned short)(forwardTime * 5);
    }
    unsigned short bTimeReal() const {
        return (unsigned short)(backwardTime * 5);
    }
    bool forwardTimeEqualsBeforeTime() const {
        return forwardAsBefore;
    }
    unsigned short eTimeReal() const {
        return (unsigned short)(expTime * 5);
    }
    unsigned short iTimeReal() const {
        return (unsigned short)(5 * idleTime);
    }
    double iVoltageReal() const {
        return iVoltageConvert(ionizationVoltage);
    }
    double CPReal() const {
        return CPConvert(CPVoltage);
    }
    double eCurrentReal() const {
        return eCurrentConvert(emissionCurrent);
    }
    double hCurrentReal() const {
        return hCurrentConvert(heatCurrent);
    }
    double fV1Real() const {
        return fVConvert(focusVoltage1);
    }
    double fV2Real() const {
       return fVConvert(focusVoltage2);
    }
    double scanVoltageReal(unsigned short step) const {
        return (double)(scanVoltage(step) * 5 * 600) / 4096;
    }

    bool serialize(QDomElement& elem, QDomDocument& doc) const;
    bool deserialize(const QDomElement& elem);

    bool equals(const CommonOptions& other) const {
        return (expTime == other.expTime && idleTime == other.idleTime && ionizationVoltage == other.ionizationVoltage &&
                CPVoltage == other.CPVoltage && emissionCurrent == other.emissionCurrent && heatCurrent == other.heatCurrent &&
                focusVoltage1 == other.focusVoltage1 && focusVoltage2 == other.focusVoltage2 && beforeTime == other.beforeTime &&
                backwardTime == other.backwardTime && forwardTime == other.forwardTime && forwardAsBefore == other.forwardAsBefore);
    }
};

#endif // COMMONOPTIONS_H
